package com.google.code.jbauble.test.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.code.jbauble.mvc.Controller;
import com.google.code.jbauble.mvc.Result;
import com.google.code.jbauble.mvc.annotations.RequestMapping;
import com.google.code.jbauble.mvc.support.UrlHelper;
import com.google.code.jbauble.test.dao.UserDao;
import com.google.code.jbauble.test.model.User;
import com.google.code.jbauble.test.service.UserService;

public class UserController extends Controller {
	private UserDao dao = null;
	public UserController() {
		dao = new UserDao();
		service = (UserService)getBean("userService");
	}
	
	public Result index() {
		return render(dao.findAll());
	}
	
	private final int count = 10000;
	
	public Result init() {
		long tick = (new Date()).getTime();
		dao.deleteAll();
		
		Date dt = new Date();
		
		for (int i=0; i<count; i++) {
			User u = new User();
			u.setAge(i);
			u.setCreateTime(dt);
			u.setMail("m_" + i + "@test.com");
			u.setName("u_" + i);
			
			dao.save(u);
		}
		
		tick = (new Date()).getTime() - tick;
		
		Map m = new HashMap();
		m.put("time", tick);
		return render(m);
	}
	
	@RequestMapping(method=UrlHelper.HTTP_GET)
	public Result find() {
		return render();
	}
	
	@RequestMapping(method=UrlHelper.HTTP_POST)
	public Result find(String name, int age) {
		List<User> list = service.findByNameAndGqAge(name, age, 0, 10);
		
		Map m = getModel();
		m.put("users", list);
		m.put("name", name);
		return render(m);
	}
	
	private UserService service = null;
}
